package logic

import (
	"context"
	"srv/srv"

	"api/internal/svc"
	"api/internal/types"
	"github.com/golangblogs/gojwt"
	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.Srv.Login(l.ctx, &srv.LoginRequest{
		Account: req.Account,
		SendSms: req.SendSms,
	})
	j := jwt.NewJWT("2211a_mjh123")
	claims := jwt.CustomClaims{
		ID: uint(login.UserId),
	}
	token, err := j.CreateToken(claims)
	if err != nil {
		return nil, err
	}
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil

	}

	return &types.Response{
		Code: 500,
		Msg:  "验证码错误次数超过上限，请重新发送新的验证码",
		Data: map[string]interface{}{
			"UserId": login.UserId,
			"token":  token,
		},
	}, nil
}
